﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;

namespace JTActiveSafety.Gateway.Abstractions
{
 
    /// <summary>
    /// 
    /// </summary>
    public static class JTActiveSafetySessionExtensions
    {
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="session"></param>
        ///// <param name="data"></param>
        ///// <param name="cancellationToken"></param>
        //public static async void SendAsync(this IJTActiveSafetySession session, byte[] data, System.Threading.CancellationToken cancellationToken = default)
        //{
        //    if (data == null || session==null || session.Client == null) return;
        //    try
        //    {
        //        if (session.TransportProtocolType == JTActiveSafetyTransportProtocolType.tcp)
        //        {
        //            if (session.Client.Connected)
        //                await session.Client.SendAsync(data, SocketFlags.None, cancellationToken);
        //        }
        //        else
        //        {
        //            await session.Client.SendToAsync(data, SocketFlags.None, session.RemoteEndPoint);
        //        }
        //    }
        //    catch(SocketException ex)
        //    {
        //        //cancellationToken.ThrowIfCancellationRequested();
        //        //Error Code https://docs.microsoft.com/zh-cn/windows/win32/winsock/windows-sockets-error-codes-2
        //        var x = ex.ErrorCode; //10054 对等方重置连接。
        //    }
        //    catch (Exception)
        //    {
        //        //throw ex;
        //    }
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="session"></param>
        ///// <param name="data"></param>
        //public static void Send(this IJTActiveSafetySession session, byte[] data)
        //{
        //    if (data == null) return;
 
        //    try
        //    {
        //        if (session.TransportProtocolType == JTActiveSafetyTransportProtocolType.tcp)
        //        {
        //            if (session.Client.Connected)
        //                session.Client.Send(data, SocketFlags.None);
        //        }
        //        else
        //        {
        //            session.Client.SendTo(data, SocketFlags.None, session.RemoteEndPoint);
        //        }
        //    }
        //    catch (SocketException ex)
        //    {
        //        //cancellationToken.ThrowIfCancellationRequested();
        //        //Error Code https://docs.microsoft.com/zh-cn/windows/win32/winsock/windows-sockets-error-codes-2
        //        var x = ex.ErrorCode; //10054 对等方重置连接。
        //    }
        //    catch (Exception ex)
        //    {
        //        //throw ex;
        //    }
        //}

        //public static async void SendAsync(this IJTActiveSafetySession session, ReadOnlyMemory<byte> buffer,  System.Threading.CancellationToken cancellationToken = default)
        //{
        //    if (session.TransportProtocolType == JTActiveSafetyTransportProtocolType.tcp)
        //    {
        //        if (session.Client.Connected)
        //            await session.Client.SendAsync(buffer, SocketFlags.None,cancellationToken);
        //    }
        //    else
        //    {
        //        await session.Client.SendToAsync(buffer.ToArray(), SocketFlags.None, session.RemoteEndPoint);
        //    }
        //}

        //public static void Send(this IJTActiveSafetySession session, ReadOnlySpan<Byte> data)
        //{
        //    if (data == null) return;
        //    if (session.TransportProtocolType == JTActiveSafetyTransportProtocolType.tcp)
        //    {
        //        if (session.Client.Connected)
        //            session.Client.Send(data, SocketFlags.None);
        //    }
        //    else
        //    {
        //        session.Client.SendTo(data.ToArray(), SocketFlags.None, session.RemoteEndPoint);
        //    }
        //}
    }
}
